﻿using System;
using System.Xml;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

namespace InfoJetSoft.Service.Worker
{
	
	class XmlxTextListDeleteItemWorker
	{
		
		public static void deleteItem(Form form, XmlToEditContext context)
		{
			//CID 0236 //2009.03.04 //Begin
			if( context.XmlToEdit.MultiSelectListBoxRef != null  )
			{
				doDeleteMultiSelectListItem(form, context);
			}
			//CID 0236 //2009.03.04 //End
			else
			{
				int itemSize = XmlXmlToEditItemWorker.parseItem(context);
				if (itemSize <= 1)
				{
					return;
				}
				doDeleteItem(form, context);
			}
		}

		private static void doDeleteMultiSelectListItem(Form form, XmlToEditContext context)
		{
			string oldItemValue = form.ParamTable[ JetConstant.XDOC_PARAM_MULTI_SELECT_LIST_ITEM_VALUE ] as string;
			if( oldItemValue == null )
			{
				throw new InfoJetSoft.Service.InfoJetException( "Cannot find the new item value.", "XXTLNIW01" );
			}
			//CID 0236 //2009.03.04 //Begin
			//CID 0546 //2010.05.11 //Begin
			string innerContextId = form.ParamTable[ JetConstant.XDOC_PARAM_MULTI_SELECT_LIST_CONTEXT_ID ] as string;
			if( innerContextId == null )
			{
				throw new InfoJetSoft.Service.InfoJetException( "Cannot find the context id.", "XXTLNIW02" );
			}
			//CID 0546 //2010.05.11 //End
			//CID 0236 //2009.03.04 //End
			XmlNode contextNode = DOMUtil.findNodeByJetId( form, innerContextId );
			XmlNodeList itemNodeList = DOMUtil.selectNodes( contextNode, context.XmlToEdit.MultiSelectListBoxRef, 0, form.XmlNsResolver );
			if( itemNodeList.Count > 0 )
			{
				bool found = false;
				foreach( XmlNode itemNode in itemNodeList )
				{
					string itemValue = DOMUtil.getNodeValue( itemNode );
					if( itemValue.Equals( oldItemValue ) )
					{
						XmlElement parentElement = (XmlElement) itemNode.ParentNode;
						XmlElement itemElement = (XmlElement) itemNode;
						if (SchemaUtil.canRemoveChild(form, parentElement, itemElement))
						{
							//需要在Remvoe之前计算Path,不能移动.
							string path = DOMUtil.getNodePath( itemElement );
							
                            //CID 0907 //2011.05.13 //Begin
                            //DeleteContext
                            //CID 1015 //2011.09.07 //Begin
                            DeleteContext deleteContext = FormCalculatedFieldWorker.CreateDeleteContext(form, parentElement);
                            parentElement.RemoveChild(itemElement);
                            FormCalculatedFieldWorker.calcaulteGroupNode(form, itemElement, deleteContext, false);
                            //CID 1015 //2011.09.07 //End
                            //CID 0907 //2011.05.13 //End
							FormCodeDispatchWorker.OnAfterChange( form, path, itemElement, FormCodeDispatchWorker.Updated, false );
						}
						found = true;
					}
				}
				if( !found )
				{
					throw new InfoJetException( "Cannot find the item node.", "XXTLDIW01" );
				}
			}
			else
			{
				throw new InfoJetException( "Cannot find the item node.", "XXTLDIW01" );
			}
		}
		
		private static void doDeleteItem(Form form, XmlToEditContext context)
		{
			XmlDocument doc = context.Doc;
			
			XmlNode itemNode = context.ItemNode;
			XmlNode parentNode = itemNode.ParentNode;
			if (parentNode is XmlElement)
			{
				XmlElement parentElement = (XmlElement) parentNode;
				if (itemNode is XmlElement)
				{
					XmlElement itemElement = (XmlElement) itemNode;
					if (SchemaUtil.canRemoveChild(form, parentElement, itemElement))
					{
						//需要在Remvoe之前计算Path,不能移动.
						string path = DOMUtil.getNodePath( itemElement );
						
						//CID 0907 //2011.05.13 //Begin
                        //DeleteContext
                        //CID 1015 //2011.09.07 //Begin
                        DeleteContext deleteContext = FormCalculatedFieldWorker.CreateDeleteContext(form, parentElement);
                        parentElement.RemoveChild(itemElement);
                        FormCalculatedFieldWorker.calcaulteGroupNode(form, itemElement, deleteContext, false);
                        //CID 1015 //2011.09.07 //End
                        //CID 0907 //2011.05.13 //End
                        FormCodeDispatchWorker.OnAfterChange(form, path, itemElement, FormCodeDispatchWorker.Updated, false);
					}
				}
			}
		}
	}
}